package com.feiyang.ni.test;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class MyFrame3 extends JFrame implements KeyListener {
    public MyFrame3(){
        this.setSize(488, 500);
        this.setTitle("检查测试");
        //设置窗口置顶
        this.setAlwaysOnTop(true);
        //设置窗口居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.addKeyListener(this);
        //显示（建议放在最后）
        this.setVisible(true);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("你按住不放");
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int code = e.getKeyCode();
        if(code == KeyEvent.VK_UP){
            System.out.println("你按了上键");
        }
        if(code == KeyEvent.VK_DOWN){
            System.out.println("你按了下键");
        }
        if(code == KeyEvent.VK_LEFT){
            System.out.println("你按了左键");
        }
        if(code == KeyEvent.VK_RIGHT){
            System.out.println("你按了右键");
        }
    }
}
